<?php 
function acceptJobRegistration($applicant_id,$job_id)
{
	$phql = "UPDATE job_apply_applicant SET status = 'For Interview' WHERE job_id = {$job_id} and applicant_id = {$applicant_id}";

	//get company info
	$phql2 = "SELECT * 
			  FROM job_order jo 
			  LEFT JOIN employer e ON e.employer_id = jo.employer_id
			  WHERE jo.job_id = {$job_id}";
	
	$result1 = 	mysql_query($phql);

	
	$row = mysql_fetch_array(mysql_query($phql2));


	return insertNotif2($applicant_id,"accepted",$row['ename'].' has accepted your job registration',"");
}

function hireApplicant($applicant_id,$job_id,$emp_id)
{
	$phql = "UPDATE job_apply_applicant SET status = 'For Hiring' WHERE job_id = {$job_id} and applicant_id = {$applicant_id}";
	//get company info
	$phql2 = "SELECT * 
			  FROM job_order jo 
			  LEFT JOIN employer e ON e.employer_id = jo.employer_id
			  WHERE jo.job_id = {$job_id}";
	
	$result1 = mysql_query($phql);
	
	
	$row = mysql_fetch_array(mysql_query($phql2));


	return insertNotif2($applicant_id,"accepted",$row['ename'].' hired you',"");
}


function declineJobRegistration($applicant_id,$job_id)
{
	$phql = "UPDATE job_apply_applicant SET status = 'Declined' WHERE job_id = {$job_id} and applicant_id = {$applicant_id}";

	//get company info
	$phql2 = "SELECT * 
			  FROM job_order jo 
			  LEFT JOIN employer e ON e.employer_id = jo.employer_id
			  WHERE jo.job_id = {$job_id}";
	
	$result1 = 	mysql_query($phql);

	
	$row = mysql_fetch_array(mysql_query($phql2));


	return insertNotif2($applicant_id,"declined",$row['ename'].' has declined your job registration',"");
}


function listEmployerPostJobRegistration()
{
	$phql = "SELECT e.refid,e.e_statusid,e.ename,jo.employer_id,jo.job_id,ea.country,jo.status,COUNT(jo.job_id) as Job_Posted 
			 FROM `job_order` jo
			 LEFT JOIN employer e ON e.employer_id = jo.employer_id
			 LEFT JOIN e_address ea ON ea.employer_id = jo.employer_id
	 		 GROUP BY e.employer_id";

	return  mysql_query($phql);
}

function listEmployersSetForInterview(){
	$phql = "SELECT * FROM applicant_interview_date aid
			 LEFT JOIN applicant a ON aid.applicant_id = a.applicant_id
			 LEFT JOIN job_order jo ON jo.job_id = aid.job_id
			 LEFT JOIN employer e ON e.employer_id = jo.employer_id";
	return  mysql_query($phql);			 
}

function getName($employer_id)
{

	$phql="SELECT * FROM employer WHERE employer_id = '{$employer_id}'";

	$list = mysql_query($phql);	
	
	$name = '';
	while($row=mysql_fetch_array($list))
	{								 
		$name = $row['hr_name'];
	}
	return $name;
}



function getEmail($employer_id)
{

	$phql="SELECT * FROM employer WHERE employer_id = '{$employer_id}'";

	$list = mysql_query($phql);	
	
	$email = '';
	while($row=mysql_fetch_array($list))
	{								 
		$email = $row['email'];
	}
	return $email;
}


function listEmployerSignUp()
{
	$phql = "SELECT e.email,e.e_statusid,e.hr_name,e.employer_id,ea.country,e.contact 
			 FROM employer e
			 LEFT JOIN e_address ea ON ea.employer_id = e.employer_id
			 WHERE e.e_statusid = 1
	 		 GROUP BY e.employer_id
	 		 ORDER BY e.ename ASC";
	return mysql_query($phql);
}

function getTotalEmployersPages()
{
	$phql = "SELECT COUNT(*) as num
      FROM accounts a
      LEFT JOIN employer e ON a.emailadd = e.email
      WHERE a.level_no = 2 and e.e_statusid != 4";
    return mysql_fetch_array(mysql_query($phql))['num'];
}

function listEmployersAccount($start,$limit)
{
  $phql = "SELECT e.email,e.ename,a.user,a.account_id,e.employer_id
      FROM accounts a
      LEFT JOIN employer e ON a.emailadd = e.email
      WHERE a.level_no = 2 and e.e_statusid != 4
      LIMIT $start,$limit
      ";
  return mysql_query($phql);
}

function setForInterview($applicant_id,$job_id,$interview_date,$type,$from,$to,$nearest)
{
	$phql = "INSERT INTO applicant_interview_date(applicant_id,job_id,interview_date,type,time_from,time_to,nearest_location) VALUES({$applicant_id},{$job_id},'{$interview_date}','{$type}','{$from}','{$to}','{$nearest}')";
	$phql2 = "SELECT * 
			  FROM job_order jo 
			  LEFT JOIN employer e ON e.employer_id = jo.employer_id
			  WHERE jo.job_id = {$job_id}";
	
	$result1 = 	mysql_query($phql);
	$row = mysql_fetch_array(mysql_query($phql2));


	return insertNotif2($applicant_id,"set_for_interview",$row['ename'].' set an interview, check your interview schedule below this notifcation',"");
}

function insertNotif2($receiver_id,$type,$message,$href)
{
	$phql = "INSERT INTO notif(receiver_id,type,message,href) 
			 VALUES({$receiver_id},'{$type}','{$message}','{$href}')";
	

	$result = mysql_query($phql);
	
	$message = array(
			"message" => $message,
			"receiver_id" => $receiver_id,
			"notif_id" => mysql_insert_id(),
			"date_created" => CURR_DATE
		);
	$pusher = new Pusher(PUSHER_APP_KEY, PUSHER_APP_SECRET, PUSHER_APP_ID);
	$pusher->trigger('my-channel', 'my-event', $message);
	return $result;
}

function listEmployerNotif($employer_id)
{
	$phql = "SELECT * FROM employer_notif en 
			 LEFT JOIN employer e ON e.employer_id = en.receiver_id
			 WHERE en.receiver_id = '{$employer_id}'";
	return mysql_query($phql);

}


?>